package cn.gdsdxy.controller;

import cn.gdsdxy.pojo.User;
import cn.gdsdxy.service.UserService;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
@ResponseBody
public class UserController {
    @Autowired
    UserService userService;

    @GetMapping("/user/{username}")
    public String getusername(@PathVariable("username") String username){
        if (userService.getusername(username)>0)
            return "success";
        else
            return "fail";
//            String users= JSON.toJSONString(userService.getusername(username));
//            return users;
    }
    @PutMapping("/user/register")
    public String adduser(@RequestBody User user){
        if (userService.adduser(user)>0)
            return "success";
        else
            return "fail";
    }

    @PutMapping("/user/login")
    public String checkuser(@RequestBody User user){
        if (userService.checkuser(user)>0)
            return "success";
        else
            return "fail";
    }

    @DeleteMapping("/username/{username}")//接受delete请求
    //@GetMapping("/username/{username}")//为了在浏览器中验证删除是否成功，将这里的请求方式改为Get请求
    public String deleteUser(@PathVariable("username") String username){
        if (userService.deleteUser(username)>0)
            return "注销成功，请重新登录";
        else
            return "注销失败";
    }

    @PutMapping("/updatauser")
    public String updataUser(@RequestBody User user){
        if (userService.updatauser(user)>0)
            return "success";
        else
            return "fail";
    }
}
